package com.bykj.property.sys.controller;

import com.bykj.property.controller.vo.response.ErrorCode;
import com.bykj.property.controller.vo.response.RespResult;
import com.bykj.property.controller.vo.view.SysUserVo;
import com.bykj.property.model.SysUser;
import com.bykj.property.model.TbLoginLog;
import com.bykj.property.service.SysUserService;
import com.bykj.property.service.TbLoginLogService;
import com.bykj.property.sys.service.LoginService;
import com.bykj.property.util.AddressUtil;
import com.bykj.property.util.IPUtil;
import com.bykj.property.util.Md5HashUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
* @author yiming
* @title: LoginController
* @projectName property
* @description: TODO
* @date 2019/6/1222:06
*/
@Slf4j
@Api(tags = {"登录"})
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    TbLoginLogService tbLoginLogService;
    @Autowired
    SysUserService sysUserService;



    @ApiOperation(value = "登录")

    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    public RespResult login(SysUser data, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        String username = data.getUsername();
        String password = data.getPassword();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        subject.login(token);
        Session session=SecurityUtils.getSubject().getSession();
        session.setTimeout(60000 * 60000 * 24);
        session.setAttribute("userType","0");
        SysUser user = sysUserService.findByName(username);
        user.setPassword(null);
        user.setSalt(null);
        try {
            String ipstr = IPUtil.getIpAddr(request);
            TbLoginLog tbLoginLog = new TbLoginLog();
            tbLoginLog.setLocation(AddressUtil.getAddress(ipstr));
            tbLoginLog.setIp(ipstr);
            tbLoginLog.setDevice(request.getHeader("User-Agent"));
            tbLoginLog.setUsername(user.getUsername());
            tbLoginLogService.save(tbLoginLog);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        String sessionId = (String) subject.getSession().getId();
        user.setToken(sessionId);
        session.setAttribute("currentuser",user);
        return RespResult.ok(user);
    }

    @ApiOperation(value = "退出")
    @RequestMapping(value = "/logout", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public RespResult test() {
        Subject subject = SecurityUtils.getSubject();

        subject.logout();
        return RespResult.ok();

    }

    @ApiOperation(value = "修改密码")

    @RequestMapping(value = "/updatePassword", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public RespResult updatePassword( SysUser vo,@RequestParam("oldPassword") String oldPassword) {
        SysUser user = loginService.getByUsername(vo.getUsername());
        Boolean flag = false;
        if (user.getPassword().equals(Md5HashUtil.getMd5(oldPassword, user.getSalt()))) {
            flag = true;
        }
        if (flag) {
            sysUserService.update(vo);
            return RespResult.ok();
        } else {
            return RespResult.error(ErrorCode.PASSWORD_ERROR);

        }
    }
}